define(function(require, export)
{
    var Pos = export.Pos = function(x, y)
    {
        this.x = x;
        this.y = y;
    }
    
    export.posNew = function(x, y)
    {
        return new Pos(x, y);
    }

    export.posAdd = function(p1, p2)
    {
        return new Pos(p1.x + p2.x, p1.y + p2.y);
    }

    export.posSub = function(p1, p2)
    {
        return new Pos(p1.x - p2.x, p1.y - p2.y);
    }

    export.posMul = function(p1, p2)
    {
        return new Pos(p1.x * p2.x, p1.y * p2.y);
    }

    export.posZero = function()
    {
        return new Pos(0, 0);
    }

    export.posEqual = function(pos1, pos2)
    {
        return (pos1.x == pos2.x && pos1.y == pos2.y);
    }

    var Rect = export.Rect = function(x, y, width, height)
    {
        this.x = x;
        this.y = y;
        this.widht = width;
        this.height = height;
    }

    export.rectNew = function(x, y, width, height)
    {
        return new Rect(x, y, width, height);
    }

    export.rectEqual = function(rect1, rect2)
    {
        return (rect1.x == rect2.x && rect1.y == rect2.y && 
                rect1.widht == rect2.width && rect1.height == rect2.height);
    }

    export.rectGetMinX = function(rect)
    {
        return rect.x;
    }

    export.rectGetMaxX = function(rect)
    {
        return rect.x + rect.width;
    }

    export.rectGetMinY = function(rect)
    {
        return rect.y;
    }

    export.rectGetMaxY = function(rect)
    {
        return rect.y + rect.height;
    }

    export.posInRect(pos, rect)
    {
        return ((p.x >= rect.x && p.x <= rect.x + rect.width) &&
                (p.y >= rect.y && p.y <= rect.y + rect.height));
    }

    export.rectUnion(rect1, rect2)
    {
        var rect = rectNew(0, 0, 0, 0);
        rect.x = Math.min(rect1.x, rect2.x);
        rect.y = Math.min(rect1.y, rect2.y);
        rect.width = Math.max(rect1.x + rect1.width, rect2.x + rect2.width) - rect.x;
        rect.height = Math.max(rect1.y + rect1.height, rect2.y + rect2.height) - rect.y;
        return rect;
    }

    export.rectOverlapRect(rect1, rect2)
    {
        if((rect1.x + rect1.width < rect2.x) || 
           (rect2.x + rect2.width < rect1.x) ||
           (rect1.y + rect1.height < rect2.y) ||
           (rect2.y + rect2.height < rect1.y))
        {
            return false;
        }

        return true;
    }

    export.rectIntersection(rect1, rect2)
    {
        var rect = rectNew(0, 0, 0, 0);
        rect.x = Math.max(rect1.x, rect2.x);
        rect.y = Math.max(rect1.y, rect2.y);
        rect.width = Math.min(rect1.x + rect1.width, rect2.x + rect2.width) - rect.x;
        rect.height = Math.min(rect1.y + rect1.height, rect2.y + rect2.height) - rect.y;
        
        return rect;
    }

    export.Matrix = function(xscale, yskew, xskew, yscale, xtrans, ytrans)
    {
        this.a = xscale;
        this.b = yskew;
        this.c = xskew;
        this.d = yscale;
        this.tx = xtrans;
        this.ty = ytrans;
    }

    
});